package com.bupt.secret.ca.client;

import com.bupt.secret.ca.entity.ICertService;
import com.bupt.secret.ca.entity.ICertServiceServiceLocator;
import com.bupt.secret.ca.entity.WebServiceEntity;
import com.bupt.secret.utils.PropertyUtil;
import org.apache.axis.encoding.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.ByteArrayInputStream;
import java.security.Security;
import java.security.cert.CertificateFactory;
import java.util.Date;

/**
 * @author ShiShuMin
 * @version v1.0
 * @ClassName CAClient
 * @Date: 2024/9/3 18:03
 * @Description:
 */
public class CAClient {

    public static void main(String[] args) {
        try {
            byte[] publicKeyBytes = new byte[] { 4, 29, -14, 117, 107, -38, 39,
                    -59, 22, -75, 53, 47, 16, -60, 55, -108, 89, -22, 94, -103, 17,
                    -20, 61, 5, -54, 85, -40, 102, 1, 106, 8, 126, -3, 108, 1, 90,
                    -10, 105, -5, -126, -44, 28, 28, 43, -60, 68, -10, 57, -70, 60,
                    -27, 62, -62, -90, 93, 31, -24, -35, -18, 52, 27, 105, -9, 47,
                    -34 };
//		byte[] priKeyBytes = new byte[] { 77, -22, -30, -53, -82, -88, -85,
//				-11, -55, 24, 5, -29, 54, 100, 126, 83, -58, 17, 107, -34, 84,
//				-74, 20, -62, 113, 58, 124, -5, -5, 105, 70, -58 };

            String publicKeyStr =  Base64.encode(publicKeyBytes);
            System.out.println("publicKey:"+ publicKeyStr);
            String commonName = "ceshi";


//            String configFromEnv = ApplicationPropertyUtil.getConfigFromEnv("ca.url");

            String ca = PropertyUtil.loadProperties("ca");
            System.out.println(ca);
            ICertServiceServiceLocator locator = new ICertServiceServiceLocator();
            locator.setICertServicePortEndpointAddress(ca);
            ICertService service = locator.getICertServicePort();


            //genSignatureCert生成签名证书
//            WebServiceEntity ws1=service.genSignatureCert(publicKeyStr,commonName);
//            System.out.println("signCert:"+ws1.getRoot().getCert().getSignCert());//注意这里是getSignCert

            //genSignatureCert生成加密证书(只通过设备公钥生成加密证书)
            WebServiceEntity ws2=service.genEncryptCert(publicKeyStr,null,commonName,false);
            System.out.println("p12EncryCert:"+ws2.getRoot().getCert().getP12EncryCert());//注意这里是getP12EncryCert
            System.out.println("encryptCert:"+ws2.getRoot().getCert().getEncryptCert());// 获取加密证书x509格式
            String encryptCertStr=ws2.getRoot().getCert().getEncryptCert();
            System.out.println(" 获取加密证书x509:"+encryptCertStr);
            byte[] encryptCertBytes=Base64.decode(encryptCertStr);
            System.out.println(encryptCertBytes.length);

            if (Security.addProvider(new BouncyCastleProvider()) < 0) {
                Security.removeProvider("BC");
                if (Security.addProvider(new BouncyCastleProvider()) < 0) {
                }
            }
            ByteArrayInputStream bais=new ByteArrayInputStream(encryptCertBytes);
            CertificateFactory cf= CertificateFactory.getInstance("X.509", "BC");
            java.security.cert.X509Certificate encryptCert=(java.security.cert.X509Certificate)cf.generateCertificate(bais);
            System.out.println("序列号："+encryptCert.getSerialNumber().toString(16));
            Date date1=encryptCert.getNotBefore();
            Date date2=encryptCert.getNotAfter();
            System.out.println("有效期："+date1.toString()+"--"+date2.toString());
            System.out.println("subjectDN："+encryptCert.getSubjectDN().toString());
            System.out.println("issuerDN："+encryptCert.getIssuerDN().toString());
            System.out.println("证书签名算法："+encryptCert.getSigAlgName());
            System.out.println("证书版本："+encryptCert.getVersion());

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
